﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using Microsoft.Xna.Framework.Content.Pipeline;
using System;
using System.Reflection;

namespace FxArchitect.ContentPipeline
{
    public class ContentImporterInfo
    {
        Type m_importerType;
        object m_instance;

        [Browsable(false)]
        public string TypeName { get { return m_importerType.Name; } }
        public string DefaultProcessor;
        public string DisplayName;
        public IEnumerable<string> FileExtensions;

        [Browsable(true)]
        public object Instance { get { return m_instance; } set { m_instance = value; } }

        ContentImporterInfo() { }
        public ContentImporterInfo(ContentImporterAttribute attr, Type type)
        {
            DisplayName = attr.DisplayName;
            DefaultProcessor = attr.DefaultProcessor;
            FileExtensions = attr.FileExtensions;
            m_importerType = type;
        }

        public object CreateInstance()
        {
            return m_importerType.GetConstructor(Type.EmptyTypes).Invoke(null);
        }

        public override string ToString()
        {
            return DisplayName;
        }

        public ContentImporterInfo Clone()
        {
            return new ContentImporterInfo()
            {
                m_importerType = this.m_importerType,
                DefaultProcessor = this.DefaultProcessor,
                DisplayName = this.DisplayName,
                FileExtensions = this.FileExtensions,
                m_instance = CreateInstance()
            };
        }
    }

    public class ContentImporterInfoCollection : KeyedCollection<string, ContentImporterInfo>
    {
        protected override string GetKeyForItem(ContentImporterInfo item)
        {
            return item.TypeName;
        }
    }


    public class ContentImporterConverter : ExpandableObjectConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {   // show a combobox for this element
            return true;
        }
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {   // don't allow typing the importer name
            return true;
        }
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            List<ContentImporterInfo> importerNames = new List<ContentImporterInfo>();
            foreach (ContentExtensionProject item in ContentProject.ContentExtensions)
            {
                foreach (ContentImporterInfo info in item.Importers)
                {
                    importerNames.Add(info);
                }
            }
            return new StandardValuesCollection(importerNames);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string strValue = value as string;
            if (strValue != null)
                return ContentProject.ContentExtensions.FindImporterByDisplayName(strValue);
            return base.ConvertFrom(context, culture, value);
        }
    }
}
